import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xin_he/page/mine/xin_he_mine_orders/model/xin_he_order_item_model_entity.dart';
import 'package:xin_he/page/mine/xin_he_mine_orders/xin_he_mine_order_detail/xin_he_new_order_detail_entity.dart';
import 'package:xin_he/utils/xin_he_UI_constancs.dart';

import '../../../service/xin_he_reqest.dart';
import '../../../utils/xin_he_storage.dart';
import '../../../utils/xin_he_toast.dart';
import '../../shopping_car/xin_he_instant_pay/xin_he_instant_pay_view.dart';
import 'xin_he_mine_orders_state.dart';

class XinHeMineOrdersLogic extends GetxController with GetTickerProviderStateMixin {
  final XinHeMineOrdersState state = XinHeMineOrdersState();

  List  tabList = ['全部','待付款','待发货','待收货','待评价'];
  late TabController tabsTabController;
  RxInt currentIndex = 0.obs;
  RxString orderStatus = ''.obs;
  RxBool isLoading = true.obs;
  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    currentIndex.value = Get.arguments;
    tabsTabController = TabController(vsync: this, length: tabList.length, initialIndex: currentIndex.value);

   changeTabAndRequest(currentIndex.value);
  }

  requestOrders(String type) async{

    var userId =  await XinHeLocalStorage.get(XinHeStorage.userId);

    Map<String,dynamic>map = {
      'userId':userId
    };
    if (type.isNotEmpty) {
      map['orderStatus'] = type;
    }
    isLoading.value = true;
    XinHeRequestDataResult result = await XinHeHttpRequestData.requestUniversalPost(url: '/order/queryOrder',params: map,isPost: false);

    if(!result.result){
      XinHeToast.center(result.data['message']);
      return ;
    }
    isLoading.value = false;
    List list = result.data['data'] ??[];
    List items = [];
    if(list.isNotEmpty){
      for(var e in list){
        XinHeNewOrderDetailEntity model = XinHeNewOrderDetailEntity.fromJson(e);
        items.add(model);
      }
      state.orderList.value = items;
    }else {
      state.orderList.clear();
    }
  }

  changeTabAndRequest(index) {
    if(currentIndex.value-1 == -1){
      orderStatus.value = '';
    }else  if(currentIndex.value-1 == 0){
      orderStatus.value = 'UN_PAY';
    }else if(currentIndex.value-1 == 1){
      orderStatus.value = 'PAYED';

    }else if(currentIndex.value-1 == 2){
      orderStatus.value = 'SENDED';

    }else if(currentIndex.value-1 == 3){
      orderStatus.value = 'RECEIVED';

    }
    requestOrders(orderStatus.value);

  }

  getTitle(String status) {
    String title = '';
    if(status == 'UN_PAY') {
      title = tabList[1];
    }
    if(status == 'PAYED') {
      title = tabList[2];
    }
    if(status == 'SENDED') {
      title = tabList[3];
    }
    if(status == 'RECEIVED') {
      title = tabList[4];
    }

    return title;

  }


  ///订单请求支付
  requestPayOrder() async {

    var userId =  await XinHeLocalStorage.get(XinHeStorage.userId);

    Map<String,dynamic>map = {
      'orderId':9
    };

    isLoading.value = true;
    XinHeRequestDataResult result = await XinHeHttpRequestData.requestUniversalPost(url: '/orderPay/requestPayOrder',params: map,isPostAndwwwForm: true);

    if(!result.result){
      XinHeToast.center(result.data['message']);
      return ;
    }
  }

  getRightButtonTitle(String status) {
    String title = '';
    if(status == 'UN_PAY') {
      title = '去付款';
    }
    if(status == 'PAYED') {
      title = '提醒发货';
    }
    if(status == 'SENDED') {
      title = '确认发货';
    }
    if(status == 'RECEIVED') {
      title = '去评价';
    }
    return title;
  }


  ///提醒发货
  toRemindSendAction(XinHeNewOrderDetailEntity model) async {
    var userId = await XinHeLocalStorage.get(XinHeStorage.userId);
    Map<String,dynamic> param = {
      'orderId': model.id!
    };
    XinHeRequestDataResult result = await XinHeHttpRequestData.requestUniversalPost(url: '/order/saveRemindSend',params: param,isPostAndwwwForm: true);

    if(!result.result){
      XinHeToast.center(result.data['message']);
      return ;
    }

    if(result.data['code'] == 0 ){
      XinHeToast.center('提醒成功');


    }else {
      XinHeToast.center(result.data['message']);

    }
  }


  rightButtionAction(XinHeNewOrderDetailEntity model) {
    String status = model.status!;
    if(status == 'UN_PAY') {
      toPayAction( model);
    }
    if(status == 'PAYED') {
      toRemindSendAction(model);
    }
    if(status == 'SENDED') {

    }
    if(status == 'RECEIVED') {

    }
  }


  /// 有订单的支付
  toPayAction(XinHeNewOrderDetailEntity model) async {

    Get.bottomSheet(XinHeInstantPayPage(
      zoneType: model.zoneType!,
      totalPrice:model.orderFee!,
      count: model.orderAmount!,
      order: {'orderId':model.id},
      scene: PayScene.fromGoodsOrderListItem,),
      persistent: false,isScrollControlled: true,);

  }


}
